using System;
using System.Xml;

namespace UNLV.IAP.WebControls
{
	/// <summary>
	/// Represents a comparison value used in a SqlWhereBuilder condition.
	/// </summary>
	[Serializable]
	public class SqlWhereBuilderValue
	{
		private string _name;
		private string _value;
		private string _friendlyValue;

		
		/// <summary>
		/// Name corresponding with a user input control in a ValueEntry 
		/// &lt;div&gt; section
		/// </summary>
		public string Name
		{
			get {return _name;}
			set {_name = value;}
		}

		/// <summary>
		/// The value entered or selected in a user input control in a ValueEntry
		/// &lt;div&gt; section
		/// </summary>
		public string Value
		{
			get {return _value;}
			set {_value = value;}
		}

		/// <summary>
		/// A friendly value for display, if appropriate
		/// </summary>
		public string FriendlyValue
		{
			get {return _friendlyValue;}
			set {_friendlyValue = value;}
		}

		/// <summary>
		/// Constructs a SqlWhereBuilderValue object
		/// </summary>
		public SqlWhereBuilderValue()
		{
		}


		/// <summary>
		/// Constructs a SqlWhereBuilderValue object with the given name
		/// and value.
		/// </summary>
		/// <param name="name">The name of the associated user input control</param>
		/// <param name="val">The value entered in the associated user input control</param>
		public SqlWhereBuilderValue(string name, string val)
		{
			this.Name = name;
			this.Value = val;
			this.FriendlyValue = val;
		}

		/// <summary>
		/// Utility method for deserializing SqlWhereBuilderValue from a 
		/// &lt;value&gt; node of data
		/// </summary>
		/// <param name="n">the XmlNode whose attributes will supply properties</param>
		/// <remarks>
		/// The properties should be supplied as attributes in the following format:
		/// &lt;value name='xxx' value='yyy' friendlyValue='zzz' /&gt;
		/// </remarks>
		public void DeserializeFromNode(XmlNode n)
		{
			if (n.Attributes["name"] == null) this.Name = ""; else this.Name = n.Attributes["name"].Value;
			if (n.Attributes["value"] == null) this.Value = ""; else this.Value = n.Attributes["value"].Value;
			if (n.Attributes["friendlyValue"] == null) this.FriendlyValue = this.Value; else this.FriendlyValue = n.Attributes["friendlyValue"].Value;
		}

		
		/// <summary>
		/// Constructs a SqlWhereBuilderValue object with the given name
		/// and value.
		/// </summary>
		/// <param name="name">The name of the associated user input control</param>
		/// <param name="val">The value entered in the associated user input control</param>
		/// <param name="friendlyVal">A friendly value to associate for display purposes</param>
		public SqlWhereBuilderValue(string name, string val, string friendlyVal)
		{
			this.Name = name;
			this.Value = val;
			this.FriendlyValue = friendlyVal;
		}


		/// <summary>
		/// Constructs a SqlWhereBuilderValue object taking parameters
		/// from the attributes supplied in the given &lt;value&gt;
		/// node.
		/// </summary>
		/// <param name="n"></param>
		public SqlWhereBuilderValue(XmlNode n)
		{
			DeserializeFromNode(n);
		}

		/// <summary>
		/// Returns TRUE if the given object is equal to the SqlWhereBuilderValue.
		/// </summary>
		/// <param name="obj"></param>
		/// <returns>TRUE if the object is equal to <i>this</i>, FALSE if not</returns>
		public override bool Equals(object obj)
		{
			bool retValue = true;

			SqlWhereBuilderValue v = obj as SqlWhereBuilderValue;
			if (!(object.ReferenceEquals(v, null)))
				retValue = (
					(string.Compare(this.Name, v.Name, true) == 0)
				  && (string.Compare(this.Value, v.Value, true) == 0)
				  && (string.Compare(this.FriendlyValue, v.FriendlyValue, true) == 0)
					);
			else
				retValue = false;

			return retValue;
		}

		public override int GetHashCode()
		{
			return this.ToString().GetHashCode();
		}

		/// <summary>
		/// Equality Operator
		/// </summary>
		/// <param name="v1"></param>
		/// <param name="v2"></param>
		/// <returns></returns>
		public static bool operator ==(SqlWhereBuilderValue v1, SqlWhereBuilderValue v2)
		{
			if ( (object.ReferenceEquals(v1, null))
				&& (object.ReferenceEquals(v2, null)) )
				return true;
			else if ( (object.ReferenceEquals(v1, null)) )
				return false;
			else return v1.Equals(v2);
		}

		/// <summary>
		/// Inequality Operator
		/// </summary>
		/// <param name="v1"></param>
		/// <param name="v2"></param>
		/// <returns></returns>
		public static bool operator !=(SqlWhereBuilderValue v1, SqlWhereBuilderValue v2)
		{
			if ( (object.ReferenceEquals(v1, null))
				&& (object.ReferenceEquals(v2, null)) )
				return false;
			else if ( (object.ReferenceEquals(v1, null)) )
				return true;
			else return !(v1.Equals(v2));
		}



	}
}
